#include "RtspDecoderThread.h"

RtspDecoderThread::RtspDecoderThread(QObject *parent)
    : QThread(parent)
    , decoder(new Rtsp_Decod())
    , isRunning(false) {}

RtspDecoderThread::~RtspDecoderThread() {
    closeStream();
    delete decoder;
}

void RtspDecoderThread::setUrl(const QString &url) {
    rtspUrl = url;
}

bool RtspDecoderThread::openStream() {
    if (!isRunning) {
        if (decoder->open(rtspUrl.toStdString())) {
            isRunning = true;
            start();
            return true;
        }
    }
    return false;
}

void RtspDecoderThread::closeStream() {
    if (isRunning) {
        requestInterruption();
        wait();
        decoder->close();
        isRunning = false;
    }
}

void RtspDecoderThread::decodeLoop() {
    while (!isInterruptionRequested()) {
        decoder->decode();
        // // 休眠一段时间以避免过高的CPU占用
        // QThread::msleep(30);
    }
}

void RtspDecoderThread::run() {
    decodeLoop();
}
